home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 8091 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.2 KB

  1. Path: nwgw.infi.net!usenet
  2. From: Steve Rountree <srndtree@infi.net>
  3. Newsgroups: comp.lang.c
  4. Subject: Re: How are multidimensional array stored in memory?
  5. Date: Fri, 01 Mar 1996 10:04:26 -0800
  6. Organization: InfiNet
  7. Message-ID: <31373C2A.4B00@infi.net>
  8. References: <4h6tk6$1d2@mn5.swip.net>
  9. NNTP-Posting-Host: h-cairo.dc.infi.net
  10. Mime-Version: 1.0
  11. Content-Type: text/plain; charset=us-ascii
  12. Content-Transfer-Encoding: 7bit
  13. X-Mailer: Mozilla 2.0b5 (Win16; I)
  14.  
  15. My understanding is that multi-dimensional arrays are stored 
  16. contiguously in memory.  That is that you can imagine the whole array as 
  17. a single line of data.  For example if you had an array of words:
  18.  
  19.      char words[4][31]; /* 4 words each a maximum of 30 chars + NULL */
  20.  
  21. when you reference the third element, words[2][0], you are actually 
  22. referencing *(words+62).
  23.  
  24.      words[0]: 0   -  30
  25.      words[1]: 31  -  61
  26.      words[2]: 62  -  92
  27.      words[3]: 93  -  123
  28.  
  29.   You can determine its address by multiplying the array desired by the 
  30. length or number of elements within each array.  C knows to increment the 
  31. pointer by the sizeof(pointer_type) for each element within the array, 
  32. which in this case is char.  The size of a particular data type is 
  33. machine dependent.
  34.  
  35. Steve Rountree
  36.